package tobsn.locationcalendar;

import tobsn.locationcalendar.db.DBAdapter;
import tobsn.locationcalendar.db.DBOpenHelper;
import tobsn.locationcalendar.map.TobsnMapActivity;
import tobsn.locationcalendar.service.NotificationService;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager;
import android.util.Log;

public class PONBroadcastReceiver extends BroadcastReceiver {

	private DBAdapter mDBAdapter;
	private LocationManager mLocManager;

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d("BroadcastReceiver", "Received Broadcast");

		mDBAdapter = new DBAdapter(context);
		mLocManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		Intent notificationIntent = new Intent(context,
				NotificationService.class);
		long id = intent
				.getLongExtra(TobsnMapActivity.EXTRA_NOTIFICATION_ID, 0);
		mDBAdapter.open();
		Cursor c = mDBAdapter.fetchPON(id);
		if (!c.isAfterLast()) {
			notificationIntent.putExtra("barTitle",
					c.getString(DBOpenHelper.TEXT_INT));
			notificationIntent.putExtra("contentTitle",
					c.getString(DBOpenHelper.TEXT_INT));
			String text = "latitude: " + c.getString(DBOpenHelper.LATITUDE_INT)
					+ " longitude: " + c.getString(DBOpenHelper.LONGITUDE_INT);
			notificationIntent.putExtra("contentText", text);
		}
		mDBAdapter.close();

		PendingIntent proximityIntent = PendingIntent.getBroadcast(context,
				(int) id, intent, 0);
		mLocManager.removeProximityAlert(proximityIntent);

		context.startService(notificationIntent);
	}

}
